// packageJ/coffee_machine/coffee_machineIndex/coffee_machineIndex.js
const app = getApp();
import util from "../../../utils/util";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    setTime: "",
    setTime_status: "",
    setStatus: "请选择状态",
    // 选择状态
    show: false,
    columns: ["未结算", "已结算"],
    // 订单日期
    orderShow: false,
    // 选择分润日期
    bonussShow: false,
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime(),

    name: "咖啡机",
    editShow: false,

    info: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '门店中心',
    })
    this.data.options = options;
    this.getData();
  },

  toUrl(e) {
    let {
      url,
      params
    } = e.currentTarget.dataset;
    if (params) {
      url += `?${params}`
    }
    console.log(url, params, 'dasdasd')
    wx.navigateTo({
      url: url,
    })
  },
  blurInp(e) {
    let urlStr = app.getNetAddresss("plugin.coffee-machine.frontend.owner.rename");
    app._postNetWork({
      url: urlStr,
      data: {
        machine_id: this.data.options.machine_id,
        name: this.data.name
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          app.tips('修改成功')
          this.setData({
            editShow: false
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  editTap(e) {
    this.setData({
      editShow: !this.data.editShow
    })
  },
  cancelStatusBox() {
    if (this.data.show) {
      this.setData({
        show: false
      })
    }
  },
  showTap() {
    this.setData({
      show: true
    })
  },
  bonussShowTap() {
    this.setData({
      bonussShow: true
    })
  },
  orderShowTap() {
    this.setData({
      orderShow: true
    })
  },
  onConfirm(value) {
    let {
      name,
      key
    } = value.currentTarget.dataset;
    this.setData({
      setStatus: name,
      show: false
    })
    this.getData();
    console.log(name, key)
  },
  cancelTap() {
    this.setData({
      orderShow: false,
      bonussShow: false
    })
  },
  orderShowConfirm(e) {
    let res = wx.getSystemInfoSync();
    let setTime = '';
    let value = new Date(e.detail);
    if (res.platform == 'ios') {
      setTime = util.formatTime(value);
    } else {
      setTime = util.formatTimes(value);
    }
    this.setData({
      setTime: setTime,
      orderShow: false
    })
    this.getData();
  },
  bonussShowConfirm(e) {
    let res = wx.getSystemInfoSync();
    let setTime = '';
    let value = new Date(e.detail);
    if (res.platform == 'ios') {
      setTime = util.formatTime(value);
    } else {
      setTime = util.formatTimes(value);
    }
    this.setData({
      setTime_status: setTime,
      bonussShow: false
    })
    this.getData();
  },
  bonussShowStop() {
    this.setData({
      setTime_status: ""
    })
    this.getData();
  },
  orderShowStop() {
    this.setData({
      setTime: ""
    })
    this.getData();
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  getData() {
    let json = {
      machine_id: this.data.options.machine_id,
      order_time: this.data.setTime,
      commission_time: this.data.setTime_status,
      commission_status: this.data.setStatus == "已结算" ? 2 : this.data.setStatus == "待结算" ? 1 : ""
    };
    let urlStr = app.getNetAddresss("plugin.coffee-machine.frontend.owner.machineDetail");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            name: res.data.name || '咖啡机',
            info: res.data
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
    console.log(json)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})